import {
  getCommodityType,
  addCommodityType,
  deleteCommodityType,
  updateCommodityType,
  searchCommodityType,
} from '../services/commoditytype';

export default {

  namespace: 'commoditytype',

  state: {
    commoditytypeList: [],
  },

  subscriptions: {
      setup({ dispatch, history }) {  // eslint-disable-line
    },
  },

  effects: {
      *getCommodityType({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(getCommodityType, payload);
      // console.log(9999, response);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addCommodityType({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(addCommodityType, payload);// eslint-disable-line
      // console.log(1111, response);
      yield put({
        type: 'getCommodityType',
      });
    },
    *deleteCommodityType({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(deleteCommodityType, payload);// eslint-disable-line
      // console.log(1111, response);
      yield put({
        type: 'getCommodityType',
      });
    },
    *updateCommodityType({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(updateCommodityType, payload);// eslint-disable-line
      // console.log(1111, response);
      yield put({
        type: 'getCommodityType',
      });
    },
    *searchCommodityType({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(searchCommodityType, payload);
      const res = {
        code: response.code,
        message: response.message,
        data: [response.data],
      };
      yield put({
        type: 'save',
        payload: res,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        commoditytypeList: action.payload.data };
    },
  },

};

